package com.whfc.ms.api.emp.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.RandomUtil;
import com.whfc.emp.dto.AppPayrollAttachAddDTO;
import com.whfc.emp.dto.AppPayrollDTO;
import com.whfc.emp.dto.AppPayrollDetailDTO;
import com.whfc.emp.dto.AppPayrollEmpDTO;
import com.whfc.emp.param.AppPayrollAddParam;
import com.whfc.emp.param.AppPayrollDetailEditParam;
import com.whfc.emp.param.AppPayrollListParam;
import com.whfc.emp.service.AppPayrollService;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.ms.api.emp.service.MsPayrollService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @ClasssName MsPayrollServiceImpl
 * @Description 人员工资
 * @Author hw
 * @Date 2020/11/26 14:25
 * @Version 1.0
 */
@Service
public class MsPayrollServiceImpl implements MsPayrollService {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @DubboReference(interfaceClass = AppPayrollService.class, version = "1.0.0", timeout = 5000)
    private AppPayrollService appPayrollService;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;

    private static final String HEAD = "%s%s年%s月人员工资表";

    @Override
    public PageData<AppPayrollDTO> list(AppPayrollListParam request) {
        return appPayrollService.list(request);
    }

    @Override
    public AppPayrollDetailDTO detail(Integer payrollId) {
        return appPayrollService.detail(payrollId);
    }

    @Override
    public void add(AppPayrollAddParam request) {
        appPayrollService.add(request);
    }

    @Override
    public void edit(AppPayrollDetailEditParam request) {
        appPayrollService.edit(request);
    }

    @Override
    public void del(Integer payrollId) {
        appPayrollService.del(payrollId);
    }

    @Override
    public void commit(Integer payrollId) {
        appPayrollService.commit(payrollId);
    }

    @Override
    public AppPayrollAttachAddDTO attachAdd(String filename, InputStream fis, Integer payrollId) {
        //先将文件上传到服务器上
        String fileName = filePathConfig.getFilePath("emp/payroll/file", RandomUtil.getRandomFileName(), "xls");
        String url = fileHandler.upload(fileName, fis);
        return appPayrollService.attachAdd(fileName, url, payrollId);
    }

    @Override
    public void attachDel(Integer attachId) {
        appPayrollService.attachDel(attachId);
    }

    @Override
    public OssPathDTO export(Integer payrollId) throws Exception {
        return appPayrollService.export(payrollId);
    }

    @Override
    public void importExcel(MultipartFile file, Integer payrollId, Integer clearingForm) throws IOException {
        InputStream inputStream = file.getInputStream();
        Workbook workBook;
        try {
            workBook = WorkbookFactory.create(inputStream);
        } catch (Exception e) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "读取excel失败");
        }
        List<AppPayrollEmpDTO> list = new ArrayList<>();

        Sheet sheet = workBook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            int rowIndex = row.getCell(0).getRowIndex();
            if (rowIndex < 3) {
                // 跳过前三行
                continue;
            }
            AppPayrollEmpDTO payrollEmpDTO = new AppPayrollEmpDTO();
            String idCardNo = row.getCell(2).getStringCellValue();
            payrollEmpDTO.setIdCardNo(idCardNo);

            Double workAmount = null;
            Double realAttendDays = null;
            Double attendDays = null;
            Double unitPrice = null;
            Double salaryTotal = null;
            Double salaryBorrow = null;
            Double salaryDeduct = null;
            Double salaryShould = null;
            Double salaryReal = null;
            String remark = null;
            Date date = null;
            if (clearingForm == 1) {
                attendDays = row.getCell(5).getNumericCellValue();
                realAttendDays = row.getCell(6).getNumericCellValue();
                unitPrice = row.getCell(7).getNumericCellValue();
                salaryTotal = row.getCell(8).getNumericCellValue();
                salaryBorrow = row.getCell(9).getNumericCellValue();
                salaryDeduct = row.getCell(10).getNumericCellValue();
                salaryShould = row.getCell(11).getNumericCellValue();
                salaryReal = row.getCell(12).getNumericCellValue();
                Cell cell13 = row.getCell(13);
                Cell cell14 = row.getCell(14);
                if (cell13 == null) {
                    row.createCell(13);
                }
                if (cell14 == null) {
                    row.createCell(14);
                }
                date = row.getCell(13).getDateCellValue();
                remark = row.getCell(14).getStringCellValue();
            } else {
                workAmount = row.getCell(5).getNumericCellValue();
                unitPrice = row.getCell(6).getNumericCellValue();
                salaryTotal = row.getCell(7).getNumericCellValue();
                salaryBorrow = row.getCell(8).getNumericCellValue();
                salaryDeduct = row.getCell(9).getNumericCellValue();
                salaryShould = row.getCell(10).getNumericCellValue();
                salaryReal = row.getCell(11).getNumericCellValue();
                Cell cell12 = row.getCell(12);
                Cell cell13 = row.getCell(13);
                if (cell12 == null) {
                    row.createCell(12);
                }
                if (cell13 == null) {
                    row.createCell(13);
                }
                date = row.getCell(12).getDateCellValue();
                remark = row.getCell(13).getStringCellValue();
            }
            payrollEmpDTO.setDate(date);
            if (realAttendDays != null) {
                payrollEmpDTO.setRealAttendDays(realAttendDays.intValue());
            }
            logger.info("attendDays:"+attendDays);
            if (attendDays != null) {
                payrollEmpDTO.setAttendDays(attendDays.intValue());
            }
            payrollEmpDTO.setRemark(remark);
            payrollEmpDTO.setSalaryBorrow(salaryBorrow);
            payrollEmpDTO.setSalaryReal(salaryReal);
            payrollEmpDTO.setSalaryShould(salaryShould);
            payrollEmpDTO.setSalaryTotal(salaryTotal);
            payrollEmpDTO.setUnitPrice(unitPrice);
            payrollEmpDTO.setSalaryDeduct(salaryDeduct);
            if (workAmount != null) {
                payrollEmpDTO.setWorkAmount(workAmount.intValue());
            }
            list.add(payrollEmpDTO);
        }
        appPayrollService.addList(list, payrollId);
    }

    @Override
    public ListData<AppPayrollEmpDTO> getPayrollList(Integer empId) {
        return appPayrollService.getPayrollList(empId);
    }

    @Override
    public PageData<AppPayrollEmpDTO> empPayrollList(Integer payrollId, Integer pageNum, Integer pageSize) {
        return appPayrollService.empPayrollList(payrollId, pageNum, pageSize);
    }
}
